package com.zhixi100.userservice.controller;

import com.zhixi100.framework.common.ObjectRestResponse;
import com.zhixi100.framework.utils.RestResponseUtils;
import com.zhixi100.userservice.entity.User;
import com.zhixi100.userservice.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * author:tommy
 * date:2018/12/8
 */
@RestController
@RequestMapping("/v1")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/find/user/{username}")
    public ObjectRestResponse findUserByUserName(@PathVariable("username") String username) {
        ObjectRestResponse result = RestResponseUtils.success();
        result.setData(userService.findUserByUserName(username));
        return result;
    }

    @PostMapping("/check/user")
    public ObjectRestResponse checkUser(@RequestParam("username") String username,
                                        @RequestParam("password") String password) {
        ObjectRestResponse result = RestResponseUtils.success();
        User user = new User();
        user.setUserName(username);
        user.setPassword(password);
        result.setData(userService.checkUser(user));
        return result;
    }

    //登录
    @PostMapping("/login")
    public ObjectRestResponse login(@RequestParam("username") String username,
                                    @RequestParam("password") String password,
                                    HttpServletRequest request,
                                    HttpServletResponse response) {
        ObjectRestResponse result = RestResponseUtils.success();
        User user = new User();
        user.setUserName(username);
        user.setPassword(password);
        result.setData(userService.login(user, request, response));
        return result;
    }

    //登出
    @GetMapping("/logout")
    public ObjectRestResponse logout(HttpServletRequest request) {
        request.getSession().invalidate();
        ObjectRestResponse result = RestResponseUtils.success();
        return result;
    }
}
